<?php

namespace app\controllers;

use Yii;
use app\models\TmptableModel;
use app\models\TmptableSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\db\ActiveRecord;

/**
 * TmptableController implements the CRUD actions for TmptableModel model.
 */
class TmptableController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all TmptableModel models.
     * @return mixed
     */
    public function actionIndex()
    {
    	$sql='TRUNCATE TABLE tmptable';
    	$sql2='SELECT p.stu_id,p.stu_name,p.sex,p.card_id,p.used_name,p.nation,p.political,p.address nativeplace,p.religion,p.height,p.maritial_status,p.blood_type,p.telephone,p.qq,p.blank,p.blank_num,s.enroll_time,
s.stu_type,s.major_name,s.class_name,s.status,d.dorm_area,d.dormitory,d.dorm_num,d.leader,f.address,f.recipient,f.postal_code,f.economic_status,f.load FROM personinfo p LEFT JOIN schoolinfo s ON p.stu_id=s.stu_id LEFT JOIN dorminfo d ON d.stu_id=p.stu_id LEFT JOIN familyinfo f ON f.stu_id=p.stu_id';
    	 
    	$tableName=TmptableModel::tableName();
    	$field=['stu_id','stu_name','sex','card_id','used_name','nation','political','nativeplace','religion','height','maritial_status','blood_type','telephone','qq','blank','blank_num','enroll_time',
    			'stu_type','major_name','class_name','status','dorm_area','dormitory','dorm_num','leader','address','recipient','postal_code','economic_status','load'];
    	
    	/**
    	 * 清空临时表Tmptable
    	 */
    	\Yii::$app->db->createCommand($sql)->execute();
    	/**
    	 * 获取综合查询的数据
    	 */
    	$data=ActiveRecord::findBySql($sql2)->asArray()->all();
    	/**
    	 * 批量插入 临时表Tmptable中
    	 * @var Ambiguous $totalnum
    	 */
    	$totalnum = Yii::$app->db->createCommand()->batchInsert($tableName,$field,$data)->execute();
    	
        $searchModel = new TmptableSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single TmptableModel model.
     * @param string $id
     * @return mixed
     */
//     public function actionView($id)
//     {
//         return $this->render('view', [
//             'model' => $this->findModel($id),
//         ]);
//     }

    /**
     * Creates a new TmptableModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
//     public function actionCreate()
//     {
//         $model = new TmptableModel();

//         if ($model->load(Yii::$app->request->post()) && $model->save()) {
//             return $this->redirect(['view', 'id' => $model->stu_id]);
//         } else {
//             return $this->render('create', [
//                 'model' => $model,
//             ]);
//         }
//     }

    /**
     * Updates an existing TmptableModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
//     public function actionUpdate($id)
//     {
//         $model = $this->findModel($id);

//         if ($model->load(Yii::$app->request->post()) && $model->save()) {
//             return $this->redirect(['view', 'id' => $model->stu_id]);
//         } else {
//             return $this->render('update', [
//                 'model' => $model,
//             ]);
//         }
//     }

    /**
     * Deletes an existing TmptableModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
//     public function actionDelete($id)
//     {
//         $this->findModel($id)->delete();

//         return $this->redirect(['index']);
//     }

    /**
     * Finds the TmptableModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return TmptableModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = TmptableModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
